//
//  PresentViewController.m
//  A_GOGO
//
//  Created by Liuiu on 16/1/16.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "PresentViewController.h"
#import "UIColor+Wonderful.h"
#import "PresentTableViewCell.h"
#import "HomeModel.h"
#import <UIImageView+WebCache.h>
#import "NetworkHandler.h"
#import <MMDrawerBarButtonItem.h>
#import <UIViewController+MMDrawerController.h>
#import "LeftDrawerViewController.h"
@interface PresentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,retain)UITableView *tableV;
@property (nonatomic ,retain)NSMutableArray *dataArr;
@end

@implementation PresentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view .backgroundColor =[UIColor gainsboroColor];
    self.navigationItem.title = @"礼包";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:21],
       NSForegroundColorAttributeName:[UIColor grayColor]}];
    self.navigationController.navigationBar.translucent = NO;
    
    //个人资料
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor whiteColor];
    button.frame =  CGRectMake(0, 0, 38, 38);
    button.layer.cornerRadius = 19;
    [button setBackgroundImage:[UIImage imageNamed:@"personal"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(left:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    //账号箱
    UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithTitle:@"账户箱" style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    self.navigationItem.rightBarButtonItems = @[item];
    
    //搜索
    UIView *viewa =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    viewa.backgroundColor =[UIColor lightGrayColor];
    [self.view addSubview:viewa];
    [viewa release];
    UITextField *field =[[UITextField alloc]initWithFrame:CGRectMake(10, 15, self.view.frame.size.width - 20, 30)];
    field.backgroundColor =[UIColor whiteColor];
    field.placeholder = @"                                    搜索~";
    field.layer.cornerRadius = 5;
    [self.view addSubview:field];
    [field release];
    //a Label
    UILabel *labela =[[UILabel alloc]initWithFrame:CGRectMake(25, 0, 2, self.view.frame.size.height)];
    labela.backgroundColor = [UIColor grayColor];
    [self.tableV addSubview:labela];
    [labela release];
    
//********  tableV
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width , self.view.frame.size.height - 180) style:UITableViewStylePlain];
    _tableV.backgroundColor = [UIColor whiteColor];
    _tableV.delegate = self;
    _tableV.dataSource = self;
    [self.view addSubview:_tableV];
    [_tableV release];
    
    //注册 table 重用池
    [_tableV registerClass:[PresentTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self data];
    
    
}
- (void)data
{
    NSString *str = @"http://open.youxiduo.com/gift/home/?os=ios&uid=6167230&appname=yxdjqb&debug=beta&source=58d0318827f851170da7e3937a9e0f72&pageSize=10&pageIndex=1&version=3.4.0";
      [NetworkHandler GETWithURL:str completion:^(id result) {
          self.dataArr = result[@"result"];
          self.dataArr =[HomeModel modelHanderWithArray:_dataArr];
          [_tableV reloadData];
      }];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PresentTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    HomeModel *e =_dataArr[indexPath.row];

    
    [cell.imagea sd_setImageWithURL:[NSURL URLWithString:e.url]];
    cell.gname.text = e.gname;
    cell.title.text = e.title;
    cell.last.text = e.lastcount;
    cell.card.text = e.cardcount;
    if (e.lastcount.floatValue == 0) {
        cell.receive.text = @"已领完 >";
        cell.receive.textColor = [UIColor lightGrayColor];
        
    }else{
        cell.receive.text = @"领取 >";
        cell.receive.textColor = [UIColor doderBlue];
    }
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

#pragma mark - Button区
- (void)right:(UIButton *)btn
{
    
}
- (void)left:(UIButton *)btn
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
